

**************LT Max 31-36 then sum 1-30 days
use "$path\Intermediary Data\base_estim_EG_0821.dta", clear

egen idb=group(id)

egen lt_effect=rowmax(coeff1_reg_31-coeff1_reg_36)
winsor2 lt_effect , replace  cuts(1 99) trim

foreach i of numlist 0/40{
gen y_`i'=coeff1_reg_`i'/lt_effect
}
qui  egen CIR_2=rowtotal( y_1-y_30)
qui  egen sc=rowtotal( coeff2_reg_1-coeff2_reg_30)
qui  egen sc10=rowtotal( coeff2_reg_1-coeff2_reg_10)

foreach i of numlist 2/30{
qui  egen CIR_h`i'=rowtotal( y_1-y_`i')
}

keep id CIR_h* CIR_2 lt_effect sc*
drop if CIR_2==0

duplicates drop id,  force
save "$path\Intermediary Data\base_CIR2", replace



use "$path\Intermediary Data\base_estim_EG_MA.dta", clear

egen idb=group(id)

egen lt_effect_MA=rowmax(coeff1_reg_31-coeff1_reg_36)
winsor2 lt_effect_MA , replace  cuts(1 99) trim

foreach i of numlist 0/40{
gen y_`i'=coeff1_reg_`i'/lt_effect_MA
}
qui  egen CIR_2_MA=rowtotal( y_1-y_30)

foreach i of numlist 2/30{
qui  egen CIR_h`i'_MA=rowtotal( y_1-y_`i')
}

keep id CIR_h* CIR_2_MA lt_effect_MA
drop if CIR_2_MA==0
duplicates drop id,  force
save "$path\Intermediary Data\base_CIR2_MA", replace



use "$path\Intermediary Data\base_estim_EG_brent_only.dta", clear

egen idb=group(id)

egen lt_effect_brent=rowmax(coeff1_reg_31-coeff1_reg_36)
winsor2 lt_effect_brent , replace  cuts(1 99) trim

foreach i of numlist 0/40{
gen y_`i'=coeff1_reg_`i'/lt_effect_brent
}
qui  egen CIR_2_30_brentonly=rowtotal( y_1-y_30)

rename lt_effect_brent lt_effect_brentonly
keep id CIR_2_30_brentonly lt_effect_brentonly
drop if CIR_2_30_brentonly==0
duplicates drop id,  force
save "$path\Intermediary Data\base_CIR2_brentonly_30", replace



use "$path\Intermediary Data\base_estim_EG_brent_only.dta", clear

egen idb=group(id)

egen lt_effect_brent=rowmax(coeff1_reg_25-coeff1_reg_30)
winsor2 lt_effect_brent , replace  cuts(1 99) trim

foreach i of numlist 0/40{
gen y_`i'=coeff1_reg_`i'/lt_effect_brent
}

qui  egen CIR_2_24_brentonly=rowtotal( y_1-y_24)

rename lt_effect_brent lt_effect_brentonly24
keep id CIR_2_24_brentonly lt_effect_brentonly24
drop if CIR_2_24_brentonly==0
duplicates drop id,  force
save "$path\Intermediary Data\base_CIR2_brentonly_24", replace




use "$path\Intermediary Data\base_estim_EG_0821.dta", clear
egen idb=group(id)
egen lt_effect_24=rowmax(coeff1_reg_25-coeff1_reg_30)
winsor2 lt_effect_24 , replace  cuts(1 99) trim

foreach i of numlist 0/40{
gen y_`i'=coeff1_reg_`i'/lt_effect_24
}

qui  egen CIR_2_24=rowtotal(y_1-y_24)
keep id CIR_2_24 lt_effect_24
drop if CIR_2_24==0
duplicates drop id,  force
save "$path\Intermediary Data\base_CIR2b", replace


use "$path\Intermediary Data\base_estim_EG_0821.dta", clear
egen idb=group(id)
egen lt_effect_18=rowmax(coeff1_reg_19-coeff1_reg_24)
winsor2 lt_effect_18 , replace  cuts(1 99) trim

foreach i of numlist 0/40{
gen y_`i'=coeff1_reg_`i'/lt_effect_18
}

qui  egen CIR_2_18=rowtotal(y_1-y_18)
keep id CIR_2_18 lt_effect_18
drop if CIR_2_18==0
duplicates drop id,  force
save "$path\Intermediary Data\base_CIR2c", replace


use "$path\Intermediary Data\base_estim_EG_0821.dta", clear
egen idb=group(id)
egen lt_effect_36=rowmax(coeff1_reg_36-coeff1_reg_40)
winsor2 lt_effect_36 , replace  cuts(1 99) trim

foreach i of numlist 0/40{
gen y_`i'=coeff1_reg_`i'/lt_effect_36
}
qui  egen CIR_2_36=rowtotal( y_1-y_35)
keep id CIR_2_36 lt_effect_36
drop if CIR_2_36==0
duplicates drop id,  force
save "$path\Intermediary Data\base_CIR2d", replace


clear
use "$path\Intermediary Data\base_CIR2_brentonly_30"
duplicates tag id, gen(tag)
merge 1:1 id using "$path\Intermediary Data\base_CIR2_brentonly_24"
drop _m
merge 1:1 id using "$path\Intermediary Data\base_CIR2"
drop _m
merge 1:1 id using "$path\Intermediary Data\base_CIR2_MA"
drop _m
merge 1:1 id using "$path\Intermediary Data\base_CIR2b"
drop _m
merge 1:1 id using "$path\Intermediary Data\base_CIR2c"
drop _m
merge 1:1 id using "$path\Intermediary Data\base_CIR2d"
drop _m
save "$path\Intermediary Data\base_CIR", replace

erase "$path\Intermediary Data\base_CIR2_brentonly_24.dta"
erase "$path\Intermediary Data\base_CIR2_brentonly_30.dta"
erase "$path\Intermediary Data\base_CIR2.dta"
erase "$path\Intermediary Data\base_CIR2_MA.dta"
erase "$path\Intermediary Data\base_CIR2b.dta"
erase "$path\Intermediary Data\base_CIR2c.dta"
erase "$path\Intermediary Data\base_CIR2d.dta"
